<template>
  <div class="order-container">
    <div class="order-search">
      <span class="page-title">订单列表</span>
      <el-input placeholder="请输入搜索内容" v-model="keyWords" class="input-with-select search-keyword">
        <el-select v-model="select" slot="prepend" placeholder="请选择">
          <el-option label="下单用户" value="1"></el-option>
        </el-select>
      </el-input>
      <el-button type="primary" size="small" icon="el-icon-search" @click="searchOrder">搜索</el-button>
      <el-button type="info" size="small" class="search-keyword-reset" plain @click="resetSearch">重置</el-button>
    </div>

    <div class="order-table">
      <el-table ref="multipleTable" :data="orderData" border stripe fit v-loading="listLoading"
                :row-style="{height:'50px'}" :header-row-style="{height:'40px'}">
        <el-table-column prop="id" label="id" width="80">
          <template slot-scope="scope">
            {{scope.row.id}}
          </template>
        </el-table-column>
        <el-table-column label="下单用户" width="150">
          <template slot-scope="scope">
            {{scope.row.userName}}
          </template>
        </el-table-column>
        <el-table-column label="订单编号" width="280">
          <template slot-scope="scope">
            {{scope.row.orderSn}}
          </template>
        </el-table-column>
        <el-table-column label="订单积分" width="120">
          <template slot-scope="scope">
            {{scope.row.totalAmount}}
          </template>
        </el-table-column>
        <el-table-column label="订单状态" width="150">
          <template slot-scope="scope">
            {{scope.row.status | changeStatus}}
          </template>
        </el-table-column>
        <el-table-column label="生成时间" width="160">
          <template slot-scope="scope">
            <span v-if="scope.row.time">{{scope.row.time | formatTime}}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" min-width="200" fixed="right">
          <template slot-scope="{row}">
            <el-dropdown size="mini" :split-button="true" type="primary" @click="handleLook(row)"
                         v-if="checkPermissionPoints(3) == 1">查看
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleEdit(row)" v-if="checkPermissionPoints(4) == 1">编辑
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :type="1" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                  @pagination="getOrderList"/>
    </div>

    <!--查看-->
    <el-dialog title="详细信息" :visible.sync="dialogLookVisible" width="960px" class="order-dialog look-dialog">
      <el-form label-width="140px" size="mini">
        <el-form-item label="订单编号：">
          <span>{{order.orderSn}}</span>
        </el-form-item>
        <el-form-item label="收货人姓名：">
          <span>{{order.receiverName}}</span>
        </el-form-item>
        <el-form-item label="收货人电话：">
          <span>{{order.receiverPhone}}</span>
        </el-form-item>
        <el-form-item label="收货人地址：">
          <span>{{order.receiverProvince + order.receiverCity+ order.receiverRegion + order.receiverDetailAddress}}</span>
        </el-form-item>
        <el-form-item label="订单商品：">
          <div class="order-item" v-for="item in order.mOrderItemList">
            <span>{{item.productName}}</span>
            <span>{{item.spName2}}：{{item.sp2}}</span>
          </div>
        </el-form-item>
        <el-form-item label="订单积分：">
          <span>{{order.totalAmount}}</span>
        </el-form-item>
        <el-form-item label="订单状态：">
          <span>{{order.status | changeStatus}}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" style="width: 96px" @click="dialogLookVisible = false">取消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑" :lock-scroll="true" :visible.sync="dialogEditVisible" width="960px"
               class="order-dialog">
      <el-form :model="ruleForm" ref="ruleForm" :rules="rules" label-width="auto" size="mini">
        <el-form-item label="订单编号：">
          <span>{{ruleForm.orderSn}}</span>
        </el-form-item>
        <el-form-item label="收货人姓名：">
          <span>{{ruleForm.receiverName}}</span>
        </el-form-item>
        <el-form-item label="收货人电话：">
          <span>{{ruleForm.receiverPhone}}</span>
        </el-form-item>
        <el-form-item label="收货人地址：">
          <span>{{ruleForm.receiverProvince + ruleForm.receiverCity+ ruleForm.receiverRegion + ruleForm.receiverDetailAddress}}</span>
        </el-form-item>
        <el-form-item label="订单商品：">
          <div class="order-item" v-for="item in ruleForm.mOrderItemList">
            <span>{{item.productName}}</span>
            <span>{{item.spName2}}：{{item.sp2}}</span>
          </div>
        </el-form-item>
        <el-form-item label="订单积分：">
          <span>{{ruleForm.totalAmount}}</span>
        </el-form-item>
        <el-form-item label="订单状态：" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <!--<el-radio :label=0>待付款</el-radio>-->
            <el-radio :label=1>待发货</el-radio>
            <el-radio :label=2>已发货</el-radio>
            <el-radio :label=3 disabled>已完成</el-radio>
            <el-radio :label=4 disabled>已关闭</el-radio>
            <el-radio :label=5>退货</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="success" style="width: 96px" @click="saveOrder" :loading="sendLoad">保存</el-button>
        <el-button size="small" style="width: 96px" @click="dialogEditVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
  import Pagination from '@/components/Pagination';
  import {mapGetters} from 'vuex';
  import {permissionPoints} from '@/utils/permission';
  import {formatDate} from "@/utils/index";
  import {getInitOrderList, getOrderDetail, modifyOrder} from "../../../api/shop/shop";

  export default {
    name: "order",
    computed: {
      ...mapGetters([
        'points'
      ])
    },
    components: {
      Pagination
    },
    data() {
      return {
        total: 0,
        listQuery: {
          page: 1,
          limit: 20,
          userName: '',
        },
        keyWords: '',
        select: '1',
        listLoading: false,
        dialogLookVisible: false,
        dialogEditVisible: false,
        orderData: [],
        order: {},
        ruleForm: {
          id: '',
          status: '1'
        },
        orderStatus: '',
        rules: {
          status: [
            {required: true, message: '请选择订单状态', trigger: 'change'}
          ]
        },
        sendLoad: false
      }
    },
    created() {
      this.getOrderList();
    },
    methods: {
      searchOrder() {//搜索
        if (this.keyWords == '' || this.keyWords == undefined) {
          return this.$message.info({message: '请输入搜索内容', duration: 1000});
        }
        this.listQuery.userName = '';
        if (this.select == 1) {
          this.listQuery.userName = this.keyWords;
        }
        this.listQuery.page = 1;
        this.getOrderList();
      },
      resetSearch() {
        this.select = '1';
        this.keyWords = '';
        this.listQuery.userName = '';
        this.listQuery.page = 1;
        this.getOrderList();
      },
      checkPermissionPoints(type) {
        let pointObj = this.points.order;
        return permissionPoints(type, pointObj);
      },
      getOrderList() {
        this.listLoading = true;
        getInitOrderList(this.listQuery).then((res) => {
          this.listLoading = false;
          let retObj = res;
          if (retObj.code == 0 && retObj.data != null) {
            const {items, totalRecord} = retObj.data;
            this.total = totalRecord;
            this.orderData = items;
          }
        }).catch(() => {
          this.listLoading = false;
        })
      },
      handleLook(row) {//详细信息
        this.dialogLookVisible = true;
        getOrderDetail(row.id).then((res) => {
          let retObj = res;
          if (retObj.code == 0 && retObj.data != null) {
            this.order = retObj.data;
          }
        })
      },
      handleEdit(row) {//编辑
        this.dialogEditVisible = true;
        getOrderDetail(row.id).then((res) => {
          let retObj = res;
          if (retObj.code == 0 && retObj.data != null) {
            this.ruleForm = retObj.data;
            this.orderStatus = retObj.data.status;
          }
        })
      },
      checkOrder() {
        if (this.orderStatus == 2) {
          if (this.ruleForm.status == 1) {
            this.$message.info({message: '您的订单已发货，不能改成待发货状态', duration: 1000});
            return false;
          }
          if (this.ruleForm.status == 5) {
            this.$message.info({message: '您的订单已发货，不能改成退货状态', duration: 1000});
            return false;
          }
        }
        if (this.orderStatus == 5) {
          if (this.ruleForm.status == 1) {
            this.$message.info({message: '您的订单退货，不能改成待发货状态', duration: 1000});
            return false;
          }
          if (this.ruleForm.status == 2) {
            this.$message.info({message: '您的订单退货，不能改成已发货状态', duration: 1000});
            return false;
          }
        }
        if (this.orderStatus == 4) {
          if (this.ruleForm.status == 1) {
            this.$message.info({message: '您的订单已关闭，不能改成待发货状态', duration: 1000});
            return false;
          }
          if (this.ruleForm.status == 2) {
            this.$message.info({message: '您的订单已关闭，不能改成已发货状态', duration: 1000});
            return false;
          }
          if (this.ruleForm.status == 5) {
            this.$message.info({message: '您的订单已关闭，不能改成退货状态', duration: 1000});
            return false;
          }
        }
        if (this.orderStatus == 3) {
          if (this.ruleForm.status == 1) {
            this.$message.info({message: '您的订单已完成，不能改成待发货状态', duration: 1000});
            return false;
          }
          if (this.ruleForm.status == 2) {
            this.$message.info({message: '您的订单已完成，不能改成已发货状态', duration: 1000});
            return false;
          }
          if (this.ruleForm.status == 5) {
            this.$message.info({message: '您的订单已完成，不能改成退货状态', duration: 1000});
            return false;
          }
        }
        return true;
      },
      saveOrder() {
        if (this.checkOrder()) {
          this.sendLoad = true;
          let sendObj = {};
          sendObj.id = this.ruleForm.id;
          sendObj.status = this.ruleForm.status;
          modifyOrder(sendObj).then((res) => {
            let retObj = res;
            this.sendLoad = false;
            if (retObj.code == 0 && retObj.data == 1) {
              this.$refs.ruleForm.resetFields();
              this.dialogEditVisible = false;
              this.$message.success({message: '修改成功', duration: 2000});
              this.getOrderList();
            } else {
              this.$message.error({message: '修改失败，请重试', duration: 2000});
            }
          }).catch(() => {
            this.sendLoad = false;
          })
        }
      }
    },
    filters: {
      formatTime(time) {
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss');
      },
      changeStatus(type) {
        if (type == 0) {
          return '待付款';
        } else if (type == 1) {
          return '待发货';
        } else if (type == 2) {
          return '已发货';
        } else if (type == 3) {
          return '已完成';
        } else if (type == 4) {
          return '已关闭';
        } else if (type == 5) {
          return '退货';
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .order-container {
    .order-search {
      display: flex;
      align-items: center;
      width: 100%;
      height: 80px;
      .page-title {
        padding-left: 18px;
        min-width: 160px;
        text-align: left;
        font-size: 18px;
        font-weight: bold;
        color: rgba(61, 68, 77, 1);
      }
      .search-keyword {
        margin-right: 24px;
        width: 470px;
        > > > .el-select .el-input {
          width: 110px;
        }
        > > > .el-input-group__prepend {
          background-color: #fff;
        }
      }
      .search-keyword-reset {
        margin-right: 36px;
      }
      .excel-inout-box {
        margin-left: auto;
        margin-right: 18px;
      }
    }

    .order-table {
      width: 98%;
      margin-left: 18px;
      > > > .el-table td, .el-table th {
        padding: 6px 0px;
      }
      > > > .el-table__header th {
        padding: 0;
        height: 30px;
        line-height: 30px;
      }
      > > > .el-table__fixed-right {
        height: 100% !important;
      }
      > > > .el-table__column-filter-trigger {
        line-height: 28px;
        i {
          font-size: 18px;
        }
      }
      > > > .el-table__row .cell {
        display: flex;
        align-items: center;
      }
      > > > .el-table__header .has-gutter .cell {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }

    .order-dialog > > > .el-dialog .el-dialog__header {
      display: flex;
      align-items: center;
      height: 48px;
      background: #F7F8FA;
      padding: 0px 0px 0px 12px;
      .el-dialog__headerbtn {
        top: 15px;
      }
    }
    .look-dialog > > > .el-form-item--mini.el-form-item, .el-form-item--small.el-form-item {
      margin-bottom: 0px;
    }
    .order-dialog {
      font-size: 14px;
      color: #606266;
      .order-item {
        display: flex;
        flex-direction: column;
      }
    }
    > > > .el-button {
      line-height: 1.2;
    }
  }
</style>
